package com.dyw.spzx.manager.config;

import com.dyw.spzx.manager.interceptor.LoginAuthInterceptor;
import com.dyw.spzx.manager.properties.MinioProperties;
import com.dyw.spzx.manager.properties.UserProperties;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
@EnableConfigurationProperties(MinioProperties.class)
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;
    @Autowired
    private UserProperties userProperties;

    @Autowired
    private MinioProperties minioProperties;


    @Bean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .endpoint(minioProperties
                        .getEndpointUrl())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
                .build();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                //.excludePathPatterns("/admin/system/index/login","/admin/system/index/generateValidateCode")
                .excludePathPatterns(userProperties.getNoAuthUrls())
                .addPathPatterns("/**");
    }

    // 跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 添加路径规则
                //
                .allowCredentials(true) // 是否允许在跨域的情况下传递cookie
                .allowedOriginPatterns("*") // 允许请求来源的域规则
                .allowedMethods("*") // 允许所有的请求方式
                .allowedHeaders("*"); // 允许所有的请求头
    }


}
